<!doctype html>
<html lang="en">
 <head> 
  <meta charset="UTF-8"> 
  <meta name="description" content="燎火的个人博客"> 
  <meta name="keyword" content="燎火,qq799378666,软件开发,java,c#,js,html,html5,css"> 
  <title>redis的高级实用特性讲解</title> 
  <script src="/js/jquery-1.11.1.min.js"></script> 
  <script src="/js/bootstrap-3.3.5/js/bootstrap.js"></script> 
  <link href="/js/bootstrap-3.3.5/css/bootstrap.css" rel="stylesheet"> 
  <script src="/js/base.js"></script> 
  <script src="/js/nav.js"></script> 
  <script src="/js/jquery.toTop.min.js"></script> 
  <link href="/style/article.css" rel="stylesheet"> 
 </head> 
 <body> 
  <div id="main"> 
   <h3 id="title">redis的高级实用特性讲解</h3> 
   <div id="content">
    <h2 style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: 22px; font-weight: 500; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">一、Redis高级实用特性</h2>
    <p><span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; background-color: rgb(248, 248, 248);"></span><span style="color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; background-color: rgb(248, 248, 248);"></span></p>
    <h3 style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: 18px; font-weight: 500; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">1.安全性</h3>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 设置客户端连接后进行任何其他指定前需要使用的密码。</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 警告：因为redis速度相当快，所以在一台比较好的服务器下，一个外部的用户可以在一秒钟进行150K次的密码尝试，这意味着你需要指定非常强大的密码来防止暴力破解。</p>
    <pre class="hljs elixir" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">[root@localhost&nbsp;~]#&nbsp;vi&nbsp;/usr/local/redis/etc/redis.conf</pre>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp;</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;修改配置文件中的如下配置项</p>
    <pre class="hljs markdown" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">1.&nbsp;&nbsp;#requirepass&nbsp;foobared&nbsp;&nbsp;2.&nbsp;&nbsp;requirepass&nbsp;beijing</pre>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 我们设置了连接的口令是beijing。</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 重启redis服务</p>
    <pre class="hljs perl" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">1.&nbsp;&nbsp;[root@localhost&nbsp;~]#&nbsp;pkill&nbsp;redis-server&nbsp;&nbsp;2.&nbsp;&nbsp;[root@localhost&nbsp;~]#&nbsp;/usr/local/redis/bin/redis-server&nbsp;/usr/local/redis/etc/redis.conf&nbsp;&nbsp;3.&nbsp;&nbsp;[root@localhost&nbsp;~]#&nbsp;/usr/local/redis/bin/redis-cli&nbsp;&nbsp;4.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;keys&nbsp;*&nbsp;&nbsp;5.&nbsp;&nbsp;(error)&nbsp;ERR&nbsp;operation&nbsp;not&nbsp;permitted&nbsp;&nbsp;6.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;auth&nbsp;beijing&nbsp;&nbsp;7.&nbsp;&nbsp;OK&nbsp;&nbsp;8.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;keys&nbsp;*&nbsp;&nbsp;9.&nbsp;&nbsp;1)&nbsp;"name"&nbsp;&nbsp;10.&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;exit&nbsp;&nbsp;11.&nbsp;&nbsp;&nbsp;12.&nbsp;[root@localhost&nbsp;~]#&nbsp;/usr/local/redis/bin/redis-cli&nbsp;-a&nbsp;beijing&nbsp;&nbsp;13.&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;keys&nbsp;*&nbsp;&nbsp;14.&nbsp;1)&nbsp;"name"</pre>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp;</p>
    <p><span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; background-color: rgb(248, 248, 248);"></span><span style="color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; background-color: rgb(248, 248, 248);"></span></p>
    <h3 style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: 18px; font-weight: 500; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">2.主从复制</h3>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; Redis主从复制配置和使用都非常简单。通过主从复制可以允许多个slave server拥有和master server相同的数据库副本。</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">1).Redis主从复制特点：</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; a.Master可以拥有多个slave</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; b.多个slave可以连接同一个master外，还可以连接到其它slave</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; c.主从复制不会阻塞master，在同步数据时，master可以继续处理client请求</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; d.提高系统的伸缩性</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">2).Redis主从复制过程：</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; a.Slave与Master建立连接，发送sync同步命令</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; b.Master会启动一个后台进程，将数据库快照保存到文件中，同时Master主进程会开始收集新的写命令并缓存</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; c.后台完成保存后，就将此文件发送给slave</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; d.Slave将此文件保存到硬盘上</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">3).配置主从服务器</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 配置slave服务器很简单，只需要在slave的配置文件中加入以下配置：</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">1.&nbsp;&nbsp;<span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; color: rgb(0, 128, 0);">slaveof&nbsp;192.168.1.1&nbsp;6379</span>&nbsp;#指定master的ip和端口&nbsp;&nbsp;&nbsp;</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">2.&nbsp;&nbsp;<span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; color: rgb(0, 128, 0);">masterauth&nbsp;beijing&nbsp;</span>#这是主机的密码&nbsp;&nbsp;</p>
    <pre class="hljs perl" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">1.&nbsp;&nbsp;[root@localhost&nbsp;~]#&nbsp;vi&nbsp;/usr/local/redis/etc/redis.conf&nbsp;&nbsp;2.&nbsp;&nbsp;//修改其中的slaveof&nbsp;&lt;maserip&gt;&nbsp;&lt;masterport&gt;、masterauth&nbsp;&lt;master-password&gt;&nbsp;&nbsp;3.&nbsp;&nbsp;[root@localhost&nbsp;~]#&nbsp;/usr/local/redis/bin/redis-server&nbsp;/usr/local/redis/etc/redis.conf&nbsp;&nbsp;4.&nbsp;&nbsp;[root@localhost&nbsp;~]#&nbsp;/usr/local/redis/bin/redis-cli&nbsp;-a&nbsp;beijing&nbsp;&nbsp;5.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;keys&nbsp;*&nbsp;&nbsp;6.&nbsp;&nbsp;(empty&nbsp;list&nbsp;or&nbsp;set)</pre>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 配置好后，在Master设置任何key，在Slaver数据库中也会有，表示主从复制成功。</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 如我们在主数据库上设置一对键值对</p>
    <pre class="hljs css" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">1.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;set&nbsp;name&nbsp;master&nbsp;&nbsp;
2.&nbsp;&nbsp;OK&nbsp;&nbsp;
3.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;</pre>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 在从数据库上取这个键</p>
    <pre class="hljs css" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">1.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;get&nbsp;name&nbsp;&nbsp;
2.&nbsp;&nbsp;"master"&nbsp;&nbsp;
3.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;</pre>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 我们怎么判断哪个是主哪个是从呢？我们只需调用info就可以得到主从信息，我们在从库中执行info如下：</p>
    <pre class="hljs markdown" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">1.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;info&nbsp;&nbsp;2.&nbsp;&nbsp;role:slave&nbsp;&nbsp;3.&nbsp;&nbsp;master_host:localhost&nbsp;&nbsp;4.&nbsp;&nbsp;master_port:6379&nbsp;&nbsp;5.&nbsp;&nbsp;master_link_status:up&nbsp;&nbsp;6.&nbsp;&nbsp;master_last_io_seconds_ago:10&nbsp;&nbsp;7.&nbsp;&nbsp;master_sync_in_progress:0&nbsp;&nbsp;8.&nbsp;&nbsp;db0:key=1,expires=0&nbsp;&nbsp;9.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;</pre>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 里面的role会显示是Master还是Slave服务器，master_link_status的值是up表示正在连接Master服务器</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp;</p>
    <p><span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; background-color: rgb(248, 248, 248);"></span><span style="color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; background-color: rgb(248, 248, 248);"></span></p>
    <h3 style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: 18px; font-weight: 500; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);"><strong style="box-sizing: inherit; -webkit-tap-highlight-color: transparent;">3.</strong><strong style="box-sizing: inherit; -webkit-tap-highlight-color: transparent;">事务处理</strong></h3>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; Redis对事务的支持目前不比较简单。Redis只能保证一个client发起的事务中的命令可以连续的执行，而中间不会插入其他client的命令。当一个client在一个连接中发出multi命令时，这个连接会进入一个事务上下文，该连接后续的命令不会立即执行，而是先放到一个队列中，当执行exec命令时，redis会顺序的执行队列中的所有命令。</p>
    <pre class="hljs css" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">1.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;get&nbsp;age&nbsp;&nbsp;
2.&nbsp;&nbsp;"33"&nbsp;&nbsp;
3.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;multi&nbsp;&nbsp;
4.&nbsp;&nbsp;OK&nbsp;&nbsp;
5.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;set&nbsp;age&nbsp;10&nbsp;&nbsp;
6.&nbsp;&nbsp;QUEUED&nbsp;&nbsp;
7.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;set&nbsp;age&nbsp;20&nbsp;&nbsp;
8.&nbsp;&nbsp;QUEUED&nbsp;&nbsp;
9.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;exec&nbsp;&nbsp;
10.&nbsp;1)&nbsp;OK&nbsp;&nbsp;
11.&nbsp;2)&nbsp;OK&nbsp;&nbsp;
12.&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;get&nbsp;age&nbsp;&nbsp;
13.&nbsp;"20"</pre>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 如何取消一个事务？</p>
    <pre class="hljs css" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">1.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;get&nbsp;age&nbsp;&nbsp;
2.&nbsp;&nbsp;"20"&nbsp;&nbsp;
3.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;multi&nbsp;&nbsp;
4.&nbsp;&nbsp;OK&nbsp;&nbsp;
5.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;set&nbsp;age&nbsp;30&nbsp;&nbsp;
6.&nbsp;&nbsp;QUEUED&nbsp;&nbsp;
7.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;set&nbsp;age&nbsp;40&nbsp;&nbsp;
8.&nbsp;&nbsp;QUEUED&nbsp;&nbsp;
9.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;discard&nbsp;&nbsp;
10.&nbsp;OK&nbsp;&nbsp;
11.&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;get&nbsp;age&nbsp;&nbsp;
12.&nbsp;"20"</pre>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 可以发现这次2个set age命令都没被执行。discard命令其实就是清空事务的命令队列并退出事务上下文，也就是我们常说的事务回滚。</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 如下例子可以看到，age由于是个数字，那么它可以有自增运算，但是name是个字符串，无法对其进行自增运算，所以会报错，如果按传统关系型数据库的思路来讲，整个事务都会回滚，但是我们看到redis却是将可以执行的命令提交了，所以这个现象对于习惯于关系型数据库操作的朋友来说是很别扭的，这一点也是redis今天需要改进的地方。</p>
    <pre class="hljs css" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">1.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;incr&nbsp;name&nbsp;&nbsp;
2.&nbsp;&nbsp;(error)&nbsp;ERR&nbsp;value&nbsp;is&nbsp;not&nbsp;an&nbsp;integer&nbsp;or&nbsp;out&nbsp;of&nbsp;range&nbsp;&nbsp;
3.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;multi&nbsp;&nbsp;
4.&nbsp;&nbsp;OK&nbsp;&nbsp;
5.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;incr&nbsp;age&nbsp;&nbsp;
6.&nbsp;&nbsp;QUEUED&nbsp;&nbsp;
7.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;incr&nbsp;name&nbsp;&nbsp;
8.&nbsp;&nbsp;QUEUED&nbsp;&nbsp;
9.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;exec&nbsp;&nbsp;
10.&nbsp;1)&nbsp;(integer)&nbsp;21&nbsp;&nbsp;
11.&nbsp;2)&nbsp;(error)&nbsp;ERR&nbsp;value&nbsp;is&nbsp;not&nbsp;an&nbsp;integer&nbsp;or&nbsp;out&nbsp;of&nbsp;range&nbsp;&nbsp;
12.&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;get&nbsp;age&nbsp;&nbsp;
13.&nbsp;"21"</pre>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<strong style="box-sizing: inherit; -webkit-tap-highlight-color: transparent;">乐观锁复杂事务控制</strong><strong style="box-sizing: inherit; -webkit-tap-highlight-color: transparent;">:</strong></p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 乐观锁：大多数是基于数据版本（ version）的记录机制实现的。即为数据增加一个版本标识，在基于数据库表的版本解决方案中，一般是通过为数据表添加一个"version"实段来实现读取出数据时，将此版本号一同读出，之后更新时，对此版本号加1。此时，将提交数据的版本号与数据库表对应记录的当前版本号进行比对，如果提交的数据版本号大于数据库当前版本号，则予以更新，否则认为是过期数据。</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; Redis乐观锁实例：假设有一个age的key，我们开2个session来对age进行赋值操作，我们来看一下结果如何。</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 1)第1步 session1</p>
    <pre class="hljs css" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">1.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;get&nbsp;age&nbsp;&nbsp;
2.&nbsp;&nbsp;"10"&nbsp;&nbsp;
3.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;watch&nbsp;age&nbsp;&nbsp;
4.&nbsp;&nbsp;OK&nbsp;&nbsp;
5.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;multi&nbsp;&nbsp;
6.&nbsp;&nbsp;OK</pre>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 2)第2步 session2</p>
    <pre class="hljs css" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">1.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;set&nbsp;age&nbsp;30&nbsp;&nbsp;
2.&nbsp;&nbsp;OK&nbsp;&nbsp;
3.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;get&nbsp;age&nbsp;&nbsp;
4.&nbsp;&nbsp;"30"</pre>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 3)第3步 session1</p>
    <pre class="hljs markdown" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">1.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;set&nbsp;age&nbsp;30&nbsp;&nbsp;2.&nbsp;&nbsp;QUEUED&nbsp;&nbsp;3.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;exec&nbsp;&nbsp;4.&nbsp;&nbsp;(nil)&nbsp;&nbsp;//因为有wathch&nbsp;age&nbsp;&nbsp;5.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;get&nbsp;age&nbsp;&nbsp;6.&nbsp;&nbsp;"30"</pre>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp;</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; watch命令监视给定的key，当exec时候如果监视的key从调用watch后发生过变化，则整个事务会失败。也可以调用watch多次监视多个key。这样就可以对指定的key加乐观锁了。注意watch的key是对整个连接有效的，事务也一样。如果连接断开，监视和事务都会被自动清除。当然，exec、discard、unwatch命令都会清除连接中的所有监视。</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<strong style="box-sizing: inherit; -webkit-tap-highlight-color: transparent;">事务回滚：</strong>redis的事务实现是如此简单，当然会存在一些问题。第一个问题是redis只能保证事务的每个命令连续执行，但是如果事务中的一个命令失败了，并不回滚其他命令，比如使用的命令类型不匹配。</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp;</p>
    <p><span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; background-color: rgb(248, 248, 248);"></span><span style="color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; background-color: rgb(248, 248, 248);"></span></p>
    <h3 style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: 18px; font-weight: 500; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);"><strong style="box-sizing: inherit; -webkit-tap-highlight-color: transparent;">4.</strong><strong style="box-sizing: inherit; -webkit-tap-highlight-color: transparent;">持久化机制</strong></h3>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; Redis是一个支持持久化的内存数据库，也就是说redis需要经常将内存中的数据同步到硬盘来保证持久化。</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; Redis支持两种持久化方式：</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; a.snapshotting(快照)也是默认方式</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; b.Append-only file(缩写aof)的方式</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 1).Snapshotting方式</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 快照是默认的持久化方式。这种方式是将内存中数据以快照的方式写入到二进制文件中，默认的文件名为dump.rdb。可以通过配置设置自动做快照持久化的方式。我们可以配置redis在n秒内如果超过m个key被修改就自动做快照。</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">1.&nbsp;<span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; color: rgb(0, 128, 0);">&nbsp;save&nbsp;900&nbsp;1</span>&nbsp;&nbsp;&nbsp;&nbsp;#900秒内如果超过1个key被修改，则发起快照保存&nbsp;&nbsp;</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">2.&nbsp;&nbsp;<span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; color: rgb(0, 128, 0);">save&nbsp;300&nbsp;10</span>&nbsp;&nbsp;&nbsp;#300秒内，如超过10个key被修改，则发起快照保存&nbsp;&nbsp;</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 2).aof方式</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 由于快照方式是在一定间隔时间做一次的，所以如果redis意外down掉的话，就会丢失最后一次快照后的所有修改。</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; aof比快照方式有更好的持久化性，是由于在使用aof时，redis会将每一个收到的写命令都通过write函数追加到文件中，当redis重启时会通过重新执行文件中保存的写命令来在内存中重建整个数据库的内容。</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 当然由于os会在内核中缓存write做的修改，所以可能不是立即写到磁盘上。这样aof方式的持久化也还是有可能会丢失部分修改。</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 可以通过配置文件告诉redis我们想要通过fsync函数强制os写入到磁盘的时机，默认的文件名为appendonly.aof[可通过cat方法看，保存的是命令]。</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; vi /usr/local/redis/etc/redis.conf</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">1.&nbsp;&nbsp;<span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; color: rgb(0, 128, 0);">appendonly&nbsp;yes</span>&nbsp;//启动aof持久化方式&nbsp;&nbsp;</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">2.&nbsp; #appendfsync&nbsp;always&nbsp;&nbsp;//收到写命令就立即写入磁盘，最慢，但是保证完全的持久化&nbsp;&nbsp;</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">3.&nbsp;&nbsp;<span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; color: rgb(0, 128, 0);">appendfsync&nbsp;everysec</span>&nbsp;//每秒种写入磁盘一次，在性能和持久化方面做了很好的折中&nbsp;&nbsp;</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">4.&nbsp; #appendfsync&nbsp;no&nbsp;//完全依赖os，性能最好，持久化没保证&nbsp;&nbsp;</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp;</p>
    <p><span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; background-color: rgb(248, 248, 248);"></span><span style="color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; background-color: rgb(248, 248, 248);"></span></p>
    <h3 style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: 18px; font-weight: 500; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);"><strong style="box-sizing: inherit; -webkit-tap-highlight-color: transparent;">5.</strong><strong style="box-sizing: inherit; -webkit-tap-highlight-color: transparent;">发布订阅消息</strong></h3>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 发布订阅(pub/sub)是一种消息通信模式，主要的目的是解除消息发布者和消息订阅者之间的耦合，Redis作为一个pub/sub的server，在订阅者和发布者之间起到了消息路由的功能。订阅者可以通过subscribe和psubscribe命令向redis server订阅自己感兴趣的消息类型，redis将信息类型称为通道(channel)。当发布者通过publish命令向redis server发送特定类型的信息时，订阅该信息类型的全部client都会收到此消息。</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 1)第1步 session1</p>
    <pre class="hljs markdown" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">1.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;subscribe&nbsp;tv1&nbsp;tv2&nbsp;&nbsp;2.&nbsp;&nbsp;Reading&nbsp;message...&nbsp;(press&nbsp;Ctrl+C&nbsp;to&nbsp;quit)&nbsp;&nbsp;3.&nbsp;&nbsp;1)&nbsp;"subscribe"&nbsp;&nbsp;4.&nbsp;&nbsp;2)&nbsp;"tv1"&nbsp;&nbsp;5.&nbsp;&nbsp;3)&nbsp;(integer)&nbsp;1&nbsp;&nbsp;6.&nbsp;&nbsp;1)&nbsp;"subscribe"&nbsp;&nbsp;7.&nbsp;&nbsp;2)&nbsp;"tv2"&nbsp;&nbsp;8.&nbsp;&nbsp;3)&nbsp;(integer)&nbsp;2</pre>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 2)第2步 session2</p>
    <pre class="hljs markdown" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">1.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;subscribe&nbsp;tv1&nbsp;&nbsp;2.&nbsp;&nbsp;Reading&nbsp;message...&nbsp;(press&nbsp;Ctrl+C&nbsp;to&nbsp;quit)&nbsp;&nbsp;3.&nbsp;&nbsp;1)&nbsp;"subscribe"&nbsp;&nbsp;4.&nbsp;&nbsp;2)&nbsp;"tv1"&nbsp;&nbsp;5.&nbsp;&nbsp;3)&nbsp;(integer)&nbsp;1</pre>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 3)第3步 session3</p>
    <pre class="hljs less" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">1.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;publish&nbsp;tv1&nbsp;test&nbsp;&nbsp;2.&nbsp;&nbsp;(integer)&nbsp;2&nbsp;&nbsp;//说明有两个监听&nbsp;&nbsp;3.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;&nbsp;publish&nbsp;tv2&nbsp;haha&nbsp;&nbsp;4.&nbsp;&nbsp;(integer)&nbsp;1&nbsp;&nbsp;//说明只有一个监听&nbsp;&nbsp;5.&nbsp;&nbsp;redis&nbsp;127.0.0.1:6379&gt;</pre>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 4)第4步 session1</p>
    <pre class="hljs lua" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">1.&nbsp;&nbsp;1)&nbsp;"message"&nbsp;&nbsp;2.&nbsp;&nbsp;2)&nbsp;"tv1"&nbsp;&nbsp;3.&nbsp;&nbsp;3)&nbsp;"test"&nbsp;&nbsp;4.&nbsp;&nbsp;1)&nbsp;"message"&nbsp;&nbsp;5.&nbsp;&nbsp;2)&nbsp;"tv2"&nbsp;&nbsp;6.&nbsp;&nbsp;3)&nbsp;"haha"</pre>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 第4步 session2</p>
    <pre class="hljs lua" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; padding: 2px; background-color: rgb(63, 63, 63); color: rgb(220, 220, 220); border-radius: 3px; line-height: 1.4; word-wrap: normal; font-size: 13px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace;">1.&nbsp;&nbsp;1)&nbsp;"message"&nbsp;&nbsp;2.&nbsp;&nbsp;2)&nbsp;"tv1"&nbsp;&nbsp;3.&nbsp;&nbsp;3)&nbsp;"test"</pre>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp;</p>
    <p><span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; background-color: rgb(248, 248, 248);"></span><span style="color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; background-color: rgb(248, 248, 248);"></span></p>
    <h3 style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: 18px; font-weight: 500; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);"><strong style="box-sizing: inherit; -webkit-tap-highlight-color: transparent;">6.</strong><strong style="box-sizing: inherit; -webkit-tap-highlight-color: transparent;">虚拟内存的使用</strong></h3>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; Redis的虚拟内存与操作系统的虚拟内存不是一回事，但是思路和目的都是相同的。就是暂时把不经常访问的数据从内存交换到磁盘中，从而腾出宝贵的内存空间用于其他需要访问的数据。尤其是对于redis这样的内存数据库，内存总是不够用的。除了可以将数据分割到多个redis server外，另外能够提高数据库容量的办法就是使用虚拟内存把那些不经常访问的数据交换到磁盘上。</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; 查了一下官方文档，发现虚拟内存其实一直是不建议使用的。在redis2.4版本以后，干脆就直接去掉了虚拟内存的支持，原因是虚拟内存可以使Redis在内存不够的情况下仍然可以将所有数据序列保存在内存里，这个过程很影响性能，会慢近10倍以上。感谢<strong style="box-sizing: inherit; -webkit-tap-highlight-color: transparent;">@卖红薯</strong>的提示。所以大家若想继续使用虚拟内存，则最低版本只能是redis2.4了。</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 下面是vm相关配置：</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;vi &nbsp;/usr/local/redis/etc/redis.conf</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">1.&nbsp;&nbsp;<span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; color: rgb(0, 128, 0);">vm-enabled&nbsp;yes</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#开启vm功能&nbsp;&nbsp;</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">2.&nbsp;&nbsp;<span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; color: rgb(0, 128, 0);">vm-swap-file&nbsp;/tmp/redis.swap</span>&nbsp;&nbsp;&nbsp;&nbsp;#交换出来的value保存的文件路径&nbsp;&nbsp;</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">3.&nbsp;&nbsp;<span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; color: rgb(0, 128, 0);">vm-max-memory&nbsp;1000000</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#redis使用的最大内存上限&nbsp;&nbsp;</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">4.&nbsp;&nbsp;<span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; color: rgb(0, 128, 0);">vm-page-size&nbsp;32</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#每个页面的大小32字节&nbsp;&nbsp;</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">5.&nbsp;&nbsp;<span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; color: rgb(0, 128, 0);">vm-pages&nbsp;134217728</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#最多使用多少页面&nbsp;&nbsp;</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">6.&nbsp;<span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; color: rgb(0, 128, 0);">&nbsp;vm-max-threads&nbsp;4&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#用于执行value对象换入的工作线程数量&nbsp;&nbsp;</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">&nbsp; &nbsp; &nbsp; &nbsp; 并设置如下配置，确定要使用虚拟内存</p>
    <p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Pingfang SC&quot;, STHeiti, &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; white-space: normal; background-color: rgb(248, 248, 248);">1.&nbsp;&nbsp;<span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; color: rgb(0, 128, 0);">really-use-vm&nbsp;yes</span>&nbsp;&nbsp;</p>
    <p><br></p>
   </div> 
  </div>  
 </body>
</html>